﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using moc.Models;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Reflection;
using MySql.Data.MySqlClient;
using System.Data;
using System.IO;
using System.Text.RegularExpressions;

namespace ShowSql
{
    
    class Program
    {
        
        static void Main(string[] args)
        {
            #region 输出ef查询产生的sql语句
            //var dbcontext = new MocDbContext();
            //Admin_info admin = new Admin_info()
            //{
            //    Admin_name = "q",
            //    Password = "123"
            //};
            //var query = dbcontext.Admin_info.Where(u => EF.Functions.Like(u.Admin_name, "%"))
            //    .OrderBy(u => u.Admin_id)
            //    .Skip(1)
            //    .Take(10);
            //var query1 = dbcontext.Admin_info.Where(item => EF.Functions.Like(item.Admin_name, "h%"));
            //var query2 = dbcontext.Admin_info.Where(item => item.Admin_name.Contains("h"));
            //int total = 0;
            //var query3= LoadPageEntities(10, 0, out total, u => EF.Functions.Like(u.Login_name, "%"), u => u.Login_name, true);
            //Console.WriteLine(query3.ToSql());
          #endregion           
             WriteModelsName();

           
          

            Console.ReadKey();
        }
        public static void Load() 
        {
            Assembly assembly = Assembly.LoadFrom(AppDomain.CurrentDomain.BaseDirectory + "moc.Models.dll");
            Type type = assembly.GetType("moc.Models.MocDbContext");
            PropertyInfo[] pro = type.GetProperties();
            foreach (var item in pro)
            {
                
                if (item.PropertyType == typeof(DbSet<moc.Models.MocDbContext>))
                {
                    Console.WriteLine(item.Name);
                }

            }
        }
        //把model的类名称写入txt中
        public static void WriteModelsName()
        {
            
            using (StreamWriter writer = new StreamWriter("../Lib/EntitiesNames.txt"))
            {
                MocDbContext db = new MocDbContext();
                Type type = db.GetType();
                IEnumerable<PropertyInfo> pro = type.GetProperties();
                string classNames = "";
                foreach (var item in pro)
                {
                    if (item.PropertyType.ToString().Contains("DbSet"))
                    {                      
                        classNames += item.Name + ",";
                    }
                }                
                writer.Write("");
                writer.Write(classNames);
            }
        }
        public static string UpperFirst(string s)
        {
            return Regex.Replace(s, @"\b[a-z]\w+", delegate (Match match)
            {
                string v = match.ToString();
                return char.ToUpper(v[0]) + v.Substring(1);
            });
        }
   
    }
    
}
